<?php

namespace app\crm\controller;


use app\crm\traits\Upload;
use think\Controller;
use think\Request;

class BaseController extends Controller
{
    use Upload;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        //判断用户是否登录
        if (!session('?manage_user')) {
            $this->redirect('login/index');
        }

    }

    /**
     * 上传图片
     * @return \think\response\Json
     */
    public function uploadImg()
    {
        if (!isset($_FILES['file'])) {
            return json(['code' => 0, 'msg' => '上传文件不存在']);
        }
        $file = $_FILES['file'];
        if (!$this->isImg($file['type'])) {
            return json(['code' => 0, 'msg' => '不是图片类型']);
        }
        $extensionName = $this->getExtensionName($file);
        $fileName = md5(time() . $file['name']) . '.' . $extensionName;
        $res = $this->cloudUpload($fileName, $file['tmp_name']);
        $output = $res ? ['code' => 0, 'data' => ['src' => $res]] : ['code' => 1, 'data' => '上传失败'];
        return json($output);
    }
}